#include "dialog.h"
#include "ui_dialog.h"
#include "mhttp.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QMessageBox>
Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    this->setWindowTitle("百度AI平台账号");
    on_pushButton_clicked();
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::setToken(QString &tk)
{
    ui->LeTk->setText(tk);
    ui->LeTk->setCursorPosition(0);//光标定位到开头
}

void Dialog::getIdSe(QString &id, QString &se)
{
    id=ui->LeID->text();
    se=ui->LeSe->text();
}
//获取token
void Dialog::on_pushButton_clicked()
{
    QString id=ui->LeID->text();
    QString se=ui->LeSe->text();

    MHttp h;
    QByteArray arr=QByteArray("");

    QString tokenURL=QString("https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=%1&client_secret=%2").arg(id).arg(se);
    if(h.post_sync(tokenURL,arr,arr)){
        QJsonDocument doc=QJsonDocument::fromJson(arr);
        access_token=doc.object().value("access_token").toString();
        ui->LeTk->setText(access_token);
    }else{
        QMessageBox::warning(this,"警告","获取access_token失败，请检查网络或重试！");
    }
    ui->LeTk->setCursorPosition(0);

}
